package jsp;

import util.CookieUtil;
import util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

/**
 * @Author:石小俊
 * @Date:2022/12/13 15:46
 * @Version:1.0-SNAPSHOT
 * @Description: 主页访问地址
 *  当用户访问主页时,服务器自动判断当前用户是否在cookie中保存了用户名与密码
 *  如果保存了
 *      判断当前保存的用户名与密码是否正确
 *      如果正确
 *          页面直接返回主页的页面main.jsp
 *      如果不正确
 *          页面返回登录页面,需要用户进行登录才能访问主页
 *  如果没保存
 *      页面返回登录页面,需要用户进行登录才能访问主页
 */
@WebServlet("/showMain")
public class ShowMainServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 从Cookie中取出username与password
        String username = CookieUtil.findByName("username",request);
        String password = CookieUtil.findByName("password",request);

        // 当username或者password在Cookie不存在时,跳转到登录页面
        if(StringUtil.isNull(username) || StringUtil.isNull(password)){
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
            return;
        }
        // 当Cookie中有值,且对应的值是正确的,则直接跳转主页
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/web?useUnicode=true&characterEncoding=utf-8","root","");
            conn.setAutoCommit(false);
            String sql = new StringBuffer()
                    .append(" select id,username,password,phone,address ")
                    .append(" from t_user ")
                    .append(" where username = ? ")
                    .append(" and password = ? ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            rs = ps.executeQuery();
            while(rs.next()){
                // 结果集中存在数据,则表示正确
                // 此时直接跳转到主页
                request.getRequestDispatcher("/jsp/main.jsp").forward(request,response);
                return;
            }
            // 如果结果集没有数据,则说明保存的用户名或者密码有误
            // 需要重新登录
//            response.sendRedirect(request.getContextPath()+"/jsp/login.jsp");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);

            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(ps != null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
